package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.domain.business.BusinessDO;
import com.qk.dto.business.BusinessAddDTO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessPoolListDTO;
import com.qk.dto.business.UpdateBusinessInfoDTO;
import com.qk.entity.Business;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.BusinessService;
import com.qk.management.service.impl.BusinessServiceImpl;
import com.qk.vo.business.BusinessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 商机管理控制层
 * @Author:Mr.m
 * @Date: 2025-10-15 10:58
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @GetMapping("/pool")
    public Result businessPool(
            @RequestParam(value = "businessId", required = false) Integer businessId,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "subject", required = false) Integer subject,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        // 构造BusinessPoolListDTO对象
        BusinessPoolListDTO dto = new BusinessPoolListDTO();
        dto.setBusinessId(businessId);
        dto.setPhone(phone);
        dto.setName(name);
        dto.setSubject(subject);
        dto.setPage( page);
        dto.setPageSize(pageSize);

        // 调用Service方法
        PageResult<Business> result = businessService.businessPool(dto);
        return Result.success(result);
            }
    /*
     *  跟进商机信息
     *
     * @param dto 商机信息
     * @return 跟进结果
     * */
    @LogAnno
    @PutMapping
    public Result updateBusinessInfoById(@RequestBody UpdateBusinessInfoDTO dto) {
        businessService.updateBusinessInfoById(dto);
        return Result.success();
    }

    /*
     *  根据id查询商机（回显信息）
     *
     * @param id 商机id
     * @return 转换结果
     * */
    @GetMapping("/{id}")
    public Result getBusinessInfoById(@PathVariable Integer id){
        BusinessVO vo = businessService.getBusinessInfoById(id);
        return Result.success(vo);
    }

    /*
    *  商机转客户
    *
    * @param id 商机id
    * @return 转换结果
    * */
    @LogAnno
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id){
        businessService.toCustomer(id);
        return Result.success();
    }

    /*
    *  踢回公海池
    *
    * @param id 商机id
    * @return 修改结果
    * */
    @LogAnno
    @PutMapping("/back/{id}")
    public Result back(@PathVariable Integer id) {
        businessService.updateById(Business.builder()
                .id(id)
                .status(BusinessStatusConstants.RECOVERY)
                .updateTime(LocalDateTime.now())
                .build());
        return Result.success();
    }



    /**
     * 分配商机信息
     *
     * @param businessId 商机id
     * @param userId     用户id
     * @return 修改结果
     */
    @LogAnno
    @PutMapping("/assign/{businessId}/{userId}")
    public Result updateBusinessById(@PathVariable Integer businessId, @PathVariable Integer userId) {
        businessService.updateById(Business.builder()
                .id(businessId)
                .userId(userId)
                .updateTime(LocalDateTime.now())
                .status(BusinessStatusConstants.WAIT_FOLLOW_UP)
                .build());
        return Result.success();
    }

    /**
     * 商机列表
     *
     * @param dto 商机信息
     * @return 商机列表
     */
    @GetMapping
    public Result list(BusinessListDTO dto) {
        PageResult<BusinessDO> result = businessService.businessList(dto);
        return Result.success(result);
    }

    /**
     * 新增商机
     *
     * @param dto 商机信息
     * @return 新增结果
     */
    @LogAnno
    @PostMapping
    public Result addBusiness( @RequestBody BusinessAddDTO dto) {
        businessService.addBusiness(dto);
        return Result.success();
    }
}
